//
//  CollectController.m
//  Quick_News
//
//  Created by silently on 15-2-5.
//  Copyright (c) 2015年 zkj. All rights reserved.
//

#import "CollectController.h"
#import "JokeViewController.h"
#import "JokeImageViewController.h"



@interface CollectController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) NSMutableArray *arr;
@property (nonatomic, retain) UITableView *tableView;
@end

@implementation CollectController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"我的收藏";
    // 数组初始化
    self.arr = [NSMutableArray array];
    [self.arr removeAllObjects];
    DataBaseHandler *dbHandler = [DataBaseHandler shareInstance];
    
//    NSLog(@"all%@", [dbHandler selectAll]);

    [self.arr addObjectsFromArray:[dbHandler selectAll]];

//    NSLog(@"arr%@", self.arr);

    [self.tableView reloadData];
    [self createView];
    
}


- (void)createView
{
    // 创建tableview
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 110) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    // cell分割线
//    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    
    
    
    // 刷新tableView数据
    [self.tableView reloadData];
    [self.view addSubview:self.tableView];
    [self.tableView release];
}


// tableView必须实现的2个协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
}

// 每一行要显示的cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    static NSString *str = @"reuse";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str] autorelease];
    }
    
    Jokes *jokes = [[Jokes alloc] init];
    jokes = [self.arr objectAtIndex:indexPath.row];
    cell.textLabel.text = jokes.title;
    
    return cell;
    

    
    
}

// 点击cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    JokeViewController *view = [[JokeViewController alloc] init];
    JokeImageViewController *ImageView = [[JokeImageViewController alloc] init];
    Jokes *jokes = [[[Jokes alloc] init] autorelease];
    jokes = [self.arr objectAtIndex:indexPath.row];
    view.jokes = jokes;
    ImageView.jokes = jokes;
    
    NSLog(@"imhg..%@", jokes.img);
    if (jokes.img == nil) {
        [self.navigationController pushViewController:view animated:YES];
        
    } else {
        [self.navigationController pushViewController:ImageView animated:YES];
    }
    
    [view release];

}







- (void)dealloc
{
    [_jokes release];
    [_arr release];
    [_tableView release];
    [super dealloc];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
